<?php
class Product_model extends CI_Model
{
    public $filed_items;
    public $tab_product = 'store_product';
    public $tab_prod_cate = 'store_product_category';
    public $tab_cate = 'store_category';

    function getAll($category_id = null, $offset = 0, $per_page = 9)
    {
        $this->db
            ->join($this->tab_prod_cate, "$this->tab_prod_cate.product_id = $this->tab_product.product_id")
            ->order_by("$this->tab_product.product_id", 'desc');
        if ($category_id === null) {

            return $this->db
                ->get($this->tab_product, $per_page, $offset)->result();
        } else {
            return $this->db
                ->where('category_id', $category_id)
                ->get($this->tab_product, $per_page, $offset)->result();
        }
    }

    function getAllByStatus($category_id = null, $offset = 0, $per_page = 9)
    {
        $this->db
            ->join($this->tab_prod_cate, "$this->tab_prod_cate.product_id = $this->tab_product.product_id")
            ->order_by("$this->tab_product.product_id", 'desc')
            ->where('status', 1);
        if ($category_id === null) {

            return $this->db
                ->get($this->tab_product, $per_page, $offset)->result();
        } else {
            return $this->db
                ->where('category_id', $category_id)
                ->get($this->tab_product, $per_page, $offset)->result();
        }
    }

    function save(array $data, $product_id = null)
    {
        if ($product_id === null) {
            $category_id = $data['category_id'];
            unset($data['category_id']);
            $this->db->insert($this->tab_product, $data);
            $product_id = $this->db->insert_id();

            $data = array(
                'category_id' => $category_id,
                'product_id' => $product_id
            );
            $this->db->insert($this->tab_prod_cate, $data);
        } else {
            $category_id = $data['category_id'];
            unset($data['category_id']);
            $this->db->update($this->tab_product, $data, array('product_id' => $product_id));
            $this->db->delete($this->tab_prod_cate, array('product_id' => $product_id));
            $data = array(
                'category_id' => $category_id,
                'product_id' => $product_id
            );
            $this->db->insert($this->tab_prod_cate, $data);
        }
    }

    function delete($product_id)
    {
        return $this->db->delete($this->tab_product, array('product_id' => $product_id));

    }

    function delProCate($product_id)
    {
        return $this->db->delete($this->tab_prod_cate, array('product_id' => $product_id));
    }

    function getOne($product_id)
    {
        return $this->db
            ->join($this->tab_prod_cate, "$this->tab_prod_cate.product_id =$this->tab_product.product_id ")
            ->where("$this->tab_product.product_id", $product_id)
            ->get($this->tab_product)->row();
    }

    function getByProductUrl($product_url)
    {
        return $this->db
            ->where('product_url', $product_url)
            ->get($this->tab_product)->row();
    }

    function search($keyword)
    {
        $where = "";
        $keyword = explode(" ", $keyword); //CAT CHUOI RA THANH NHIEU CHUOI NHO NGAN CACH BOI DAU CACH
        foreach ($keyword as $index => $keyword) //LAY TUNG PHAN TU CUA CHUOI
        {
            $where .= " $this->tab_product.product_name like '%{$keyword}%' AND "; // . dung de noi cac chuoi
            //echo "lan lap thu $index : where = $where<br />";
        }
        $where = trim($where);
        if (strlen($where) > 4) //TONG SO KI TU CUA CHUOI
        {
            $where = substr($where, 0, -4); //cat 4 ki tu cuoi cung
        }
        $this->db->where($where);
        return $this->getall();
    }

    function total($keyword)
    {
        $where = "";
        $keyword = explode(" ", $keyword); //CAT CHUOI RA THANH NHIEU CHUOI NHO NGAN CACH BOI DAU CACH
        foreach ($keyword as $index => $keyword) //LAY TUNG PHAN TU CUA CHUOI
        {
            $where .= " $this->tab_product.product_name like '%{$keyword}%' AND "; // . dung de noi cac chuoi
            //echo "lan lap thu $index : where = $where<br />";
        }
        $where = trim($where);
        if (strlen($where) > 4) //TONG SO KI TU CUA CHUOI
        {
            $where = substr($where, 0, -4); //cat 4 ki tu cuoi cung
        }
        $data['total'] = $this->db
            ->where($where)
            ->join($this->tab_prod_cate, "$this->tab_prod_cate.product_id = $this->tab_product.product_id")
            ->count_all_results($this->tab_product);
        return $data;
    }

    function searchByPrice($from, $to)
    {
        $this->db->where('product_price >', $from);
        $this->db->where('product_price <', $to);
        return $this->getall();
    }

    function countRows($category_id = null) //dem tat ca so bang ghi
    {
        $this->db->where('status', 1);
        if ($category_id === null) {
            return $this->db->count_all_results($this->tab_product);
        } else {
            return $this->db
                ->join($this->tab_prod_cate, "$this->tab_prod_cate.product_id = $this->tab_product.product_id")
                ->where("category_id", $category_id)
                ->count_all_results($this->tab_product);
        }
    }

    public function searchAjax(&$total, $keyword, $start, $numberget)
    {
        $where = "";
        $keyword = explode(" ", $keyword); //CAT CHUOI RA THANH NHIEU CHUOI NHO NGAN CACH BOI DAU CACH
        foreach ($keyword as $index => $keyword) //LAY TUNG PHAN TU CUA CHUOI
        {
            $where .= " $this->tab_product.product_name like '%{$keyword}%' AND "; // . dung de noi cac chuoi
            //echo "lan lap thu $index : where = $where<br />";
        }
        $where = trim($where);
        if (strlen($where) > 4) //TONG SO KI TU CUA CHUOI
        {
            $where = substr($where, 0, -4); //cat 4 ki tu cuoi cung
        }
        $data = $this->db
            ->join($this->tab_prod_cate, "$this->tab_prod_cate.product_id = $this->tab_product.product_id")
            ->order_by("$this->tab_product.product_id", 'desc')
            ->where($where)
            ->get($this->tab_product, $numberget, $start)
            ->result();

        $rs = $this->db->where($where)->count_all_results('store_product');
        $total = $rs;
        return $data;
    }

    function update_item_status($product_id, $status)
    {
        $data = array(
            'status' => (int)$status
        );

        $this->db->where('product_id', (int)$product_id);
        $this->db->update($this->tab_product, $data);
    }

    function getRules() //ham dat quy tac cho du lieu
    {
        return array(
            array(
                'field' => 'product_name', //ten truong trong data
                'label' => 'tên sản phẩm', //nhan vo van
                'rules' => 'trim|required' //bat buoc
            ),
            array(
                'field' => 'product_description', //ten truong trong data
                'label' => 'product description ', //nhan vo van
                'rules' => 'trim|required' //
            ),
            array(
                'field' => 'product_url', //ten truong trong data
                'label' => 'product url', //nhan vo van
                'rules' => 'required' //
            ),
            array(
                'field' => 'product_sub_name', //ten truong trong data
                'label' => 'product sub name', //nhan vo van
                'rules' => 'required' //
            ),
            array(
                'field' => 'product_price', //ten truong trong data
                'label' => 'product price', //nhan vo van
                'rules' => 'required|is_natural' //
            ),
            array(
                'field' => 'product_shipping', //ten truong trong data
                'label' => 'product shipping', //nhan vo van
                'rules' => 'required|is_natural' //
            ),
            array(
                'field' => 'product_image', //ten truong trong data
                'label' => 'product image', //nhan vo van
                'rules' => '' //
            ),
        );
    }

    function getImgById($id)
    {
        return $this->db
            ->select('product_image')
            ->where('product_id', $id)
            ->get($this->tab_product)->row();
    }

    function unlinkimg($img)
    {
        $url = '/product' . '/' . $img;
        return @unlink($url);
    }

}